<?php
/**
 * 房源
 */

namespace app\official\controller;


use app\official\model\Region as RegionModel;
use app\common\controller\Official;
use app\official\model\House as HouseModel;
use think\Exception;
use think\facade\Env;

class House extends Official {

    /**
     * 房源详情
     * @return false|string
     */
    public function getInfo() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) {
                return $this->resErr();
            }
            HouseModel::getInfo($params, $data, $history, $success_history);
            if (empty($data)) {
                return $this->resErr();
            } else {
                return $this->resData('success', compact('data', 'history', 'success_history'));
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 房源列表
     * @return false|string
     */
    public function houseList() {
        try {
            $params = $this->checkParams('city_code');
            if ($params === false) {
                return $this->resErr();
            }
            HouseModel::houseList($params, $page, $pageNum, $count, $data);
            return $this->resData('success', compact('page', 'pageNum', 'count', 'data'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 地图找房-地图渲染-附近3公里
     * @return false|string
     */
    public function houseMapList() {
        try {
            $params = $this->checkParams('city_code,lat,lng');
            if ($params === false) {
                return $this->resErr();
            }
            HouseModel::houseMapList($params, $data, 2);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 地图找房-列表渲染-条件-与距离无关
     * @return false|string
     */
    public function houseTableList() {
        try {
            $params = $this->checkParams('city_code,lat,lng');
            if ($params === false) return $this->resErr();
            HouseModel::houseMapList($params, $data, 1);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 地图找房-区域
     * @return false|string
     */
    public function houseMap() {
        try {
            $params = $this->checkParams('city_code');
            if ($params === false) {
                return $this->resErr();
            }
            $data = RegionModel::areaHouse($params);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 附近房源-列表渲染
     * @return false|string
     */
    public function getNearbyHouse() {
        try {
            $params = $this->checkParams('id,start,long,city_code');
            if ($params === false) {
                return $this->resErr();
            }
            $HouseModel = new HouseModel();
            $village_id = db('house')->getFieldByid($params['id'], 'village_id');
            $house_village = db('house_village')
                ->where('id', $village_id)
                ->field('x(location) as lng,Y(location) as lat')
                ->find();
            $params['lat'] = $house_village['lat'];
            $params['lng'] = $house_village['lng'];
            $HouseModel::houseMapList($params, $data, 2);//数据来自附近3公里，所有这里是2
            $data = $data['data1'];
            $start1 = (int)$params['start'];
            $long = (int)$params['long'];
            $start = ($start1 - 1) * $long;
            $data1 = array_slice($data, $start, $long);
            $count = count($data);
            if ($long > 0) $pageNum = ceil($count / $long);
            else $pageNum = 1;
            return $this->resData('success', ['count' => count($data), 'pageNum' => $pageNum, 'page' => empty($count) ? 0 : $start1, 'data' => $data1]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}
